<template>
  <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
    <el-form-item label="业务标识" prop="code" required>
      <el-input v-model="formItem.code" placeholder="请输入唯一标识" />
    </el-form-item>
    <el-form-item label="业务名称" prop="name" required>
      <el-input v-model="formItem.name" placeholder="请输入名称" />
    </el-form-item>
    <el-form-item label="模型" prop="schema">
      <treeselect
        v-model="formItem.schema"
        :multiple="false"
        :options="availableSchemas"
        :load-options="loadOptions"
        :disable-branch-nodes="true"
        id-key="code"
        label-key="name"
        placeholder="请选择类别"
        @input="onSchemaChanged"
      />
    </el-form-item>
    <el-form-item label="实例" prop="instances" required>
      <el-select v-model="formItem.instances" filterable multiple placeholder="实例">
        <el-option v-for="en in availableInstances" :key="en._id" :label="en.name" :value="en._id" />
      </el-select>
    </el-form-item>
    <el-form-item label="描述" prop="desc">
      <el-input v-model="formItem.desc" placeholder="请输入源模型描述" />
    </el-form-item>
  </el-form>
</template>

<script>
import { detail, create, update } from '@neu/cmdb-ui/api/business'
import { pureTree } from '@neu/cmdb-ui/api/schema'
import { schemaInstances } from '@neu/cmdb-ui/api/instance'

const formItem = {
  schema: null,
  code: '',
  name: '',
  desc: '',
  instances: []
}

export default {
  name: 'CmdbBusinessForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      availableSchemas: null,
      availableInstances: [],
      formItem: { ...formItem },
      // 表单验证规则
      rules: {
        category: [
          { required: true, message: '模型分类不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '唯一标识不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '模型名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    edited() {
      return this.id !== ''
    }
  },
  created() {
    if (this.id !== '') {
      this.fetch()
    }
  },
  methods: {
    fetch() {
      detail(this.id).then(response => {
        const data = response.data

        this.availableInstances = data.instances

        this.formItem.code = data.code
        this.formItem.name = data.name
        this.formItem.desc = data.desc
        this.formItem.instances = data.instances.map(it => it._id)
      })
    },
    loadOptions({ action }) {
      if (action === 'LOAD_ROOT_OPTIONS') {
        return pureTree().then(response => {
          this.availableSchemas = response.data
        })
      }
    },
    onSchemaChanged(val) {
      schemaInstances(val).then(response => {
        this.availableInstances = response.data
      })
    },
    save() {
      const updated = this.edited
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            const { code, name, desc, instances } = this.formItem
            if (updated) {
              resolve(update(this.id, { code, name, desc, instances }))
            } else {
              resolve(create({ code, name, desc, instances }))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
